import { defineStore } from 'pinia'
import $ajax from '../../api/to8050.js'
import { showSuccessMsg, showErrorMsg } from '@/utils/alterMessage.js'

export const useRoleStore = defineStore('role', {
  state: () => ({
    /** @type{ roleId: number, roleName: string,remarks: string }*/
    roles: [],
    /** @type{ roleId: number, roleName: string,remarks: string }*/
    role: { roleId: -1 },
    current_index: -1,
    // 类型将自动推断为 boolean
    dialogFormVisible: false,
    drawerVisible: false,
    sysNodes: [],
    selectedNodes: []
  }),
  getters: {
    getRole: (state) => state.role
  },
  actions: {
    setSelectedNodes(nodes) {
      // 清空数组
      this.selectedNodes.splice(0, this.selectedNodes.length)
      this.selectedNodes.push(...nodes)
    },
    setRole(role) {
      this.role = JSON.parse(JSON.stringify(role))
    },
    setDialogFormVisible(flag) {
      this.dialogFormVisible = flag
    },
    setCurrentIndex(index) {
      this.current_index = index
    },
    setDrawerVisible(flag) {
      this.drawerVisible = flag
    },
    async updateAuth(nodes) {
      const path = '/sysNode/update/auth/' + this.role.roleId
      const res = await $ajax.put(path, nodes)
      if (res != null && res.code != null && res.code === 200) {
        showSuccessMsg('权限更新成功')
      } else {
        showErrorMsg('权限更新失败')
      }
    },
    async getSysNodes() {
      if (this.sysNodes === null || this.sysNodes.length === 0) {
        const path = '/sysNode/select/allTree'
        const res = await $ajax.get(path)
        if (res != null && res.code != null && res.code === 200) {
          this.sysNodes.push(res.result)
        } else {
          showErrorMsg('节点查询失败')
        }
      }
    },
    async getRoles() {
      if (this.roles === null || this.roles.length === 0) {
        const path = '/role/select/all'
        const res = await $ajax.get(path)
        if (res != null && res.code != null && res.code === 200) {
          this.roles = res.result
          console.log('角色查询成功')
        } else {
          showErrorMsg('角色查询失败')
        }
      } else {
        return this.roles
      }
    },
    async getNodeById(roleId, treeRef) {
      if (this.selectedNodes === null || this.selectedNodes.length === 0) {
        const path = '/sysNode/select/' + roleId
        const res = await $ajax.get(path)
        if (res != null && res.code != null && res.code === 200) {
          this.selectedNodes = res.result
          treeRef.value.setCheckedKeys(this.selectedNodes, false)
        } else {
          console.log('权限查询失败')
        }
      }
    },
    handleSubmit(role) {
      if (role.roleId === -1) {
        this.addRole(role)
      } else {
        this.updateRole(role)
      }
    },
    async addRole(param) {
      const path = '/role/insert'
      const res = await $ajax.post(path, param)
      if (res != null && res.code != null && res.code === 200) {
        const role = res.result
        this.roles.push(JSON.parse(JSON.stringify(role)))
        this.role = {
          roleId: -1
        }
        showSuccessMsg('角色添加成功')
      } else {
        showErrorMsg('角色添加失败')
      }
    },
    async updateRole(param) {
      const path = '/role/update'
      const res = await $ajax.put(path, param)
      if (res != null && res.code != null && res.code === 200) {
        const role = res.result
        // console.log(role)
        this.roles[this.current_index] = JSON.parse(JSON.stringify(role))
        this.role = {
          roleId: -1
        }
        showSuccessMsg('角色修改成功')
      } else {
        showErrorMsg('角色修改失败')
      }
    },
    async deleteRole(roleId) {
      const path = '/role/delete'
      const res = await $ajax.delete(path, { params: { roleId } })
      if (res != null && res.code != null && res.code === 200) {
        this.roles.splice(this.current_index, 1)
        this.role = {
          roleId: -1
        }
        showSuccessMsg('角色删除成功')
      } else {
        showErrorMsg('角色删除失败')
      }
    }
  }
})
